﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChessTest.Chess.Items
{
    [Serializable]
    class KnightsItem : Chessman
    {
        public override string DisplayName=> "马";

        public override bool CanOriginMove(Chessboard chessboard,int selectX, int selectY)
        {
            if (!base.CanOriginMove(chessboard,selectX, selectY)) return false;
            int offsetX = Math.Abs(selectX - PositionX);
            int offsetY = Math.Abs(selectY - PositionY);

            if ((offsetX != 2 || offsetY != 1) && (offsetX != 1 || offsetY != 2))
                return false;

            if(offsetX==2 && offsetY ==1)
            {
                if (selectX > PositionX)
                {
                    if (chessboard.GetChessman(PositionX + 1, PositionY) != null)
                        return false;
                }
                else
                {
                    if (chessboard.GetChessman(PositionX - 1, PositionY) != null)
                        return false;
                }
            }

            if(offsetX ==1 && offsetY == 2)
            {
                if (selectY < PositionY)
                {
                    if (chessboard.GetChessman(PositionX, PositionY - 1) != null)
                        return false;
                }
                else
                {
                    if (chessboard.GetChessman(PositionX, PositionY + 1) != null)
                        return false;
                }
            }

                return true;

        }
        public override List<Point> GetOriginCanMoveList(Chessboard chessboard)
        {
            List<Point> list = new List<Point>();
            if (CanOriginMove(chessboard,PositionX + 2, PositionY + 1))
                list.Add(new Point(PositionX + 2, PositionY + 1));

            if (CanOriginMove(chessboard,PositionX + 2, PositionY - 1))
                list.Add(new Point(PositionX + 2, PositionY - 1));

            if (CanOriginMove(chessboard,PositionX - 2, PositionY + 1))
                list.Add(new Point(PositionX - 2, PositionY + 1));

            if (CanOriginMove(chessboard,PositionX - 2, PositionY - 1))
                list.Add(new Point(PositionX - 2, PositionY - 1));


            if (CanOriginMove(chessboard,PositionX + 1, PositionY + 2))
                list.Add(new Point(PositionX + 1, PositionY + 2));


            if (CanOriginMove(chessboard,PositionX + 1, PositionY - 2))
                list.Add(new Point(PositionX + 1, PositionY - 2));

            if (CanOriginMove(chessboard,PositionX - 1, PositionY + 2))
                list.Add(new Point(PositionX - 1, PositionY + 2));

            if (CanOriginMove(chessboard,PositionX - 1, PositionY - 2))
                list.Add(new Point(PositionX - 1, PositionY - 2));
            return list;
        }

        public override int Score => 6;

    }
}
